package com.gzx.plugin.tjzy.feign.gzxcase.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import com.gzx.plugin.tjzy.feign.gzxcase.mapper.GzxCaseDetailFeignMapper;
import com.gzx.plugin.tjzy.feign.gzxcase.mapper.GzxCaseFeignMapper;
import com.gzx.plugin.tjzy.feign.gzxcase.service.GzxCaseFeignService;
import com.gzx.tjzy.feign.req.gzxcase.GzxCaseDetailReqDto;
import com.gzx.tjzy.feign.req.gzxcase.GzxCaseReqDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;
import java.util.stream.Collectors;

import static org.apache.commons.lang.StringEscapeUtils.escapeSql;

/**
 * @author zjt
 * @description
 * @date 2025/2/13 10:01
 */
@DS("tjzy")
@Service
@RequiredArgsConstructor
public class GzxCaseFeignServiceImpl implements GzxCaseFeignService {

    private final GzxCaseFeignMapper caseFeignMapper;

    private final GzxCaseDetailFeignMapper caseDetailFeignMapper;

    /**
     * 案件主表批量新增
     *
     * @param caseReqDtoList
     */
    @Override
    public void insertBatchCase(List<GzxCaseReqDto> caseReqDtoList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxCaseReqDto>> batchList = Lists.partition(caseReqDtoList, 500);
        for (List<GzxCaseReqDto> batch : batchList) {
            String batchSql = batch.stream()
                    .map(s -> String.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                            escapeSql(s.getCompanyCode()),       // 公司编码
                            escapeSql(s.getProductCode()),       // 产品编码
                            escapeSql(s.getProjectCode()),       // 项目编码
                            escapeSql(s.getBatchNumber()),       // 批次号
                            escapeSql(String.valueOf(s.getCaseStatus())),                   // 案件状态
                            escapeSql(s.getDebtorName()),        // 借款人姓名
                            escapeSql(String.valueOf(s.getDebtorSex())),                     // 欠款人性别
                            escapeSql(s.getDebtorPhone()),       // 借款人手机号
                            escapeSql(s.getDebtorCardId()),      // 欠款人身份证号
                            escapeSql(String.valueOf(s.getIntentTag())),         // 用户标签
                            escapeSql(String.valueOf(s.getPenaltyInterest())),   // 总罚息
                            escapeSql(String.valueOf(s.getLoanAmount())),        // 总借款金额
                            escapeSql(String.valueOf(s.getOverdueAmount())),     // 总逾期金额
                            escapeSql(String.valueOf(s.getCleanLoanAmount())),   // 应催总金额
                            escapeSql(String.valueOf(s.getOverdueInterest())),   // 总逾期利息
                            escapeSql(String.valueOf(s.getOverdueFees())),       // 总逾期费用
                            escapeSql(String.valueOf(s.getOverdueStatus())),     // 逾期状态
                            escapeSql(String.valueOf(s.getMediatorId())),        // 调解员ID
                            escapeSql(s.getMediatorName()),      // 调解员姓名
                            escapeSql(s.getAffiliatedCompany()), // 所属公司
                            escapeSql(s.getPartnerBusinessId()),  // 甲方系统唯一ID
                            escapeSql("1543842934270394368"),  // 租户ID
                            escapeSql(currentTime)  // 创建时间
                    ))
                    .collect(Collectors.joining(", "));
            caseFeignMapper.insertBatch(batchSql.replace("'null'", "null"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    /**
     * 案件明细批量新增
     *
     * @param caseDetailReqDtoList
     */
    @Override
    public void insertBatchCaseDetail(List<GzxCaseDetailReqDto> caseDetailReqDtoList) {
        String currentTime = CommonDateUtil.getCurrentTimeStr();
        List<List<GzxCaseDetailReqDto>> batchList = Lists.partition(caseDetailReqDtoList, 500);
        for (List<GzxCaseDetailReqDto> batch : batchList) {
            String batchSql = batch.stream().map(s -> String.format("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                            escapeSql(s.getDebtorCardId()),       // 业务用户ID
                            escapeSql(s.getBatchNumber()),       // 批次号
                            escapeSql(s.getLoanNo()),            // 进件编号
                            escapeSql(s.getContractNo()),        // 合同编号
                            escapeSql(s.getLoanKey()),           // 借款编号
                            escapeSql(s.getFunder()),            // 资金方编码
                            escapeSql(String.valueOf(s.getTotalPeriod())),// 总期数
                            escapeSql(s.getLoanDate()),          // 订单放款日期
                            escapeSql(String.valueOf(s.getLoanAmount())),// 订单放款金额
                            escapeSql(String.valueOf(s.getOverdueAmount())),// 逾期金额
                            escapeSql(s.getOverdueDate()),       // 逾期日期
                            escapeSql(String.valueOf(s.getContractRate())),// 合同利率
                            escapeSql(s.getBankAccount()),       // 收款账户
                            escapeSql(s.getBankName()),          // 收款账户归属银行
                            escapeSql(s.getPartnerBusinessId()), // 甲方系统唯一ID
                            escapeSql(s.getAllDetail()),          // JSON详情
                            escapeSql("1543842934270394368"), // 租户ID
                            escapeSql(currentTime)                // 创建时间
                    ))
                    .collect(Collectors.joining(", "));
            caseDetailFeignMapper.insertBatch(batchSql.replace("'null'", "null"));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }
}
